package org.example.traing.controller;


import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.example.traing.OSS.OSSService;
import org.example.traing.entity.JobDetail;
import org.example.traing.entity.User;
import org.example.traing.entity.UserDetail;
import org.example.traing.service.UserService;
import org.example.traing.util.JWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/User")
public class UserController {


    @Autowired
    UserService userService;

    @GetMapping("/tokens")
    public ResponseEntity<Object> getUser(@Param("uid") String uid,
                                          @Param("password") String password){

        User user=new User(uid,password);

        User user1=userService.Login(user);
        Map<String, Object> response = new HashMap<>();
        if (user1!=null){



            response.put("msg", "success");
            response.put("data",user1);
            response.put("token",JWT.jwt(uid));
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }

    }


    @PostMapping("/tokens")
    public ResponseEntity<Object> addUser(@RequestBody User user){


        Map<String, Object> response = new HashMap<>();
        if (userService.addUser(user)!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }

    }


    @PutMapping("/tokens")
    public ResponseEntity<Object> updateUser( @RequestBody User user){


        Map<String, Object> response = new HashMap<>();
        if (userService.updateUser(user)!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }

    }



    @DeleteMapping("/tokens/{uid}")
    public ResponseEntity<Object> deleteUser(@PathVariable String uid) {

        Map<String, Object> response = new HashMap<>();
        if (userService.deleteUser(uid) != 0) {

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        } else {

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/details/{uid}")
    public ResponseEntity<Object> getUserDetail(@PathVariable String uid) {

        System.out.println("uid:"+uid);
        UserDetail userDetail = userService.getUserDetail(uid);
        OSSService ossService=new OSSService();
        URL url = ossService.getURL(uid);
        userDetail.setUserImageUrl(url);
        Map<String, Object> response = new HashMap<>();
        response.put("msg", "success");
        response.put("data",userDetail);
        return ResponseEntity.ok(response);
    }

    @PutMapping("/details")
    public ResponseEntity<Object> putUserDetail(@ModelAttribute UserDetail userDetail,
                                                @RequestParam("file") MultipartFile file) {

        System.out.println("controllerUserDetail:"+userDetail);
        UserDetail updateUserDetail =userService.updateUserDetail(userDetail);

        System.out.println("controller"+updateUserDetail);
        OSSService ossService=new OSSService();
       ossService.postImage(file,userDetail.getUid());
        Map<String, Object> response = new HashMap<>();
        if (updateUserDetail!=null){

            response.put("msg", "success");
            response.put("data",updateUserDetail);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/treasures/{uid}")
    public ResponseEntity<Object> getTreasures(@PathVariable String uid) {

        List<JobDetail> jobTreasures = userService.getTreasureJob(uid);
        Map<String, Object> response = new HashMap<>();
        if (jobTreasures!=null){

            response.put("msg", "success");
            response.put("data",jobTreasures);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }




    @PostMapping("/treasures/{uid}/{jobId}")
    public ResponseEntity<Object> addTreasures(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.addTreasure(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @DeleteMapping("/treasures/{uid}/{jobId}")
    public ResponseEntity<Object> deleteTreasures(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.deleteTreasure(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/meetings/{uid}")
    public ResponseEntity<Object> getMeeting(@PathVariable String uid) {

        List<JobDetail> jobMeeting = userService.getMeetingJob(uid);
        Map<String, Object> response = new HashMap<>();
        if (jobMeeting!=null){

            response.put("msg", "success");
            response.put("data",jobMeeting);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }




    @PostMapping("/meetings/{uid}/{jobId}")
    public ResponseEntity<Object> addMeeting(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.addMeeting(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @DeleteMapping("/meetings/{uid}/{jobId}")
    public ResponseEntity<Object> deleteMeeting(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.deleteMeeting(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/sends/{uid}")
    public ResponseEntity<Object> getSend(@PathVariable String uid) {

        List<JobDetail> jobMeeting = userService.getSendJob(uid);
        Map<String, Object> response = new HashMap<>();
        if (jobMeeting!=null){

            response.put("msg", "success");
            response.put("data",jobMeeting);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }




    @PostMapping("/sends/{uid}/{jobId}")
    public ResponseEntity<Object> addSend(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.addSend(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @DeleteMapping("/sends/{uid}/{jobId}")
    public ResponseEntity<Object> deleteSend(@PathVariable String uid,@PathVariable String jobId) {

        int x =userService.deleteSend(uid,jobId);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }





}
